package com.lecture.springlecture.event.java;

import java.util.ArrayList;
import java.util.List;

//事件发布器
public class TaskFinishEventPublisher {

    private List<TaskFinishEventListener> listeners = new ArrayList();

    //注册监听器
    public synchronized void register(TaskFinishEventListener listner) {
        if (!listeners.contains(listner)) {
            listeners.add(listner);
        }
    }

    //移除监听器
    public synchronized boolean remove(TaskFinishEventListener listener) {
        return listeners.remove(listener);
    }


    //发布任务结束事件
    public void publishEvent(TaskFinishEvent event) {

        for (TaskFinishEventListener listener : listeners) {
            listener.onTaskFinish(event);
        }
    }
}
